package com.apo1o.cmscore.controller;


import com.apo1o.cmscore.dao.SlideshowDao;
import com.apo1o.cmscore.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import com.apo1o.cmscore.service.ISlideshowService;
import com.apo1o.cmscore.bean.Slideshow;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Apo1o
 * @since 2023-11-15
 */
@Api(tags = "")
@RestController
@RequiredArgsConstructor
@RequestMapping("/slideshow")
public class SlideshowController {
    private final ISlideshowService slideshowService;
        private final SlideshowDao slideshowDao;

    @ApiOperation("查询所有可用的")
    @GetMapping("queryAllEnable")
    public Result queryAll(){
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Slideshow::getDeleted,0);
       return Result.success( slideshowDao.selectList(lqw));

    }
    @ApiOperation("根据id查询")
    @GetMapping("getSlideshowById/{id}")
    public Result queryById(@PathVariable Integer id){
        return Result.success(slideshowDao.selectById(id));
    }


    @ApiOperation(value = "新增或更新轮播图", notes = "slideshow参数包 含id值则为更新，不包含i为新增")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Slideshow slideshow) {
        if (slideshow.getId()!=null){
            slideshowDao.updateById(slideshow);
            return Result.success("更新成功");
        }
        slideshow.setUploadTime(LocalDateTime.now());
    slideshowDao.insert(slideshow);
        return Result.success("添加成功");
    }



    @ApiOperation(value = "批量删除轮播图", notes = "需要提供多个id值")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteSlideshowInBatch(@PathVariable("ids") List<Integer> ids) {
    slideshowDao.deleteBatchIds(ids);
        return Result.success();
    }


    @ApiOperation("分页查询")
    @GetMapping("/query")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,String status,String desc) {
        IPage<Slideshow> p = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Slideshow> qw = new
                LambdaQueryWrapper<>();
        qw.eq(StringUtils.hasText(status), Slideshow::getStatus,
                        status)
                .like(StringUtils.hasText(desc),
                        Slideshow::getDescription, desc)
                .orderByDesc(Slideshow::getUploadTime);
//执行分页查询
        slideshowDao.selectPage(p, qw);
        return Result.success(p);
    }

}

